package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.core.enums.OrderSysTagEnum;
import com.weixing.mall.provider.api.model.enums.OrderTypeEnum;
import com.weixing.mall.provider.mapper.DayReportProductMapper;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.enums.AfterSalesTypeEnum;
import com.weixing.mall.provider.model.vo.report.product.ProductSaleDetailVo;
import com.weixing.mall.provider.model.vo.report.product.ProductSaleReportVo;
import com.weixing.mall.provider.model.vo.report.product.ProductSaleVolumeDetailVo;
import com.weixing.mall.provider.model.vo.report.product.ProductSaleVolumeVo;
import com.weixing.mall.provider.service.IDayReportProductService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author jzy
 * @date 2020/6/8
 */
@Service
public class DayReportProductServiceImpl extends ServiceImpl<DayReportProductMapper, Order> implements IDayReportProductService {

    @Override
    public IPage<ProductSaleReportVo> productSaleReport(Long catId, String styleNo, String goodsSn, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("date_format(o.audit_time,'%Y-%m-%d') date,goods.goods_name,goods.goods_img,goods.goods_sn,goods.style_no,goods.cat_name,goods.commodity_code,sysUser.real_name,sum(goods.quantity) saleQuantity");
        q.like(null != catId,"goods.cat_id",catId);
        q.like(StrUtil.isNotEmpty(styleNo),"goods.style_no",styleNo);
        q.like(StrUtil.isNotEmpty(goodsSn),"goods.goods_sn",goodsSn);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(o.audit_time,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(o.audit_time,'%Y-%m-%d')",endTime);
        //只筛选审核完成的订单
        q.isNotNull("o.audit_time");
        q.isNotNull("o.order_type");
        q.groupBy("date_format(o.audit_time,'%Y-%m-%d')","goods.goods_id","sample.created_by");
        return baseMapper.productSaleStatus(page,q);
    }

    @Override
    public IPage<ProductSaleDetailVo> productSaleDetail(Integer orderMin, Integer orderMax, Integer stockoutMin, Integer stockoutMax, Integer cancelMin, Integer cancelMax,
                                                        Integer returnMin, Integer returnMax, BigDecimal priceMin, BigDecimal priceMax, Integer orderType,
                                                        Long actId, String color, String startTime, String endTime,String goodsSn, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        String condition="goods.goods_id=goods1.goods_id ";
        if (actId!=null){
            condition +=" and o1.act_id="+actId;
        }
        if (StrUtil.isNotEmpty(startTime)){
            condition +=" and date_format(o1.audit_time,'%Y-%m-%d')>='"+startTime+"' ";
        }
        if (StrUtil.isNotEmpty(endTime)){
            condition +=" and date_format(o1.audit_time,'%Y-%m-%d')<='"+endTime+"' ";
        }
        q.select("goods.goods_name,goods.goods_img,goods.partition_name,gg.goods_sys,gg.gs_season,goods.cat_name,goods.goods_sn,goods.commodity_code" +
                ",goods.market_price,goods.price,o.order_price discountPrice,o.created_time,o.audit_time" +
                ",group_concat(distinct goods.color ORDER BY goods.color Asc) color" +
                ",group_concat(distinct goods.size ORDER BY goods.size Asc) size" +
                ",group_concat(distinct concat(goods.size,'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id left join omc_after_sale afterSale1 on o1.id=afterSale1.order_id where "+condition+" and goods.size=goods1.size and o1.audit_time IS NOT NULL),')') ORDER BY goods.size Asc) quantitySize" +
                ",group_concat(distinct concat(goods.color,'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id left join omc_after_sale afterSale1 on o1.id=afterSale1.order_id where "+condition+" and goods.color=goods1.color and o1.audit_time IS NOT NULL),')') ORDER BY goods.size Asc) quantityColor" +
                ",count(0) quantity" +
                ",group_concat(distinct concat(if(o.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +" ,goods.size,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id where "+condition+" and goods.size=goods1.size and o1.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +"),')') ORDER BY goods.size Asc) stockoutQuantitySize" +
                ",group_concat(distinct concat(if(o.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +",goods.color,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id where "+condition+" and goods.color=goods1.color and o1.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +"),')') ORDER BY goods.size Asc) stockoutQuantityColor" +
                ",count(if(o.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +",true,null)) stockoutQuantity" +
                ",group_concat(distinct concat(if(o.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +",goods.size,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id where "+condition+" and goods.size=goods1.size and o1.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +"),')') ORDER BY goods.size Asc) cancelQuantitySize" +
                ",group_concat(distinct concat(if(o.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +",goods.color,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id where "+condition+" and goods.color=goods1.color and o1.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +"),')') ORDER BY goods.size Asc) cancelQuantityColor" +
                ",count(if(o.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +",true,null)) cancelQuantity" +
                ",group_concat(distinct concat(if(afterSale.after_sale_type="+ AfterSalesTypeEnum.RETURN_GOODS.getValue() +",goods.size,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id left join omc_after_sale afterSale1 on o1.id=afterSale1.order_id where "+condition+" and goods.size=goods1.size and afterSale1.after_sale_type="+ AfterSalesTypeEnum.RETURN_GOODS.getValue() +"),')') ORDER BY goods.size Asc) returnQuantitySize" +
                ",group_concat(distinct concat(if(afterSale.after_sale_type="+ AfterSalesTypeEnum.RETURN_GOODS.getValue() +",goods.color,null),'(',(select count(0) from omc_order o1 left join omc_order_goods goods1 on o1.id = goods1.order_id left join omc_after_sale afterSale1 on o1.id=afterSale1.order_id where "+condition+" and goods.color=goods1.color and afterSale1.after_sale_type="+ AfterSalesTypeEnum.RETURN_GOODS.getValue() +"),')') ORDER BY goods.size Asc) returnQuantityColor" +
                ",count(if(afterSale.after_sale_type="+ AfterSalesTypeEnum.RETURN_GOODS.getValue() +",true,null)) returnQuantity" +
                "");
        q.eq(actId!=null,"o.act_id",actId);
        q.eq(orderType!=null,"o.order_type",orderType);
        q.isNotNull("o.order_type");
        q.eq(StrUtil.isNotEmpty(goodsSn),"goods.goods_sn",goodsSn);
        q.like(StrUtil.isNotEmpty(color),"goods.color",color);
        q.ge(priceMin!=null,"goods.price",priceMin);
        q.le(priceMax!=null,"goods.price",priceMax);
        q.having(orderMin!=null,"quantity>="+orderMin);
        q.having(orderMax!=null,"quantity<="+orderMax);
        q.having(stockoutMin!=null,"stockoutQuantity>="+stockoutMin);
        q.having(stockoutMax!=null,"stockoutQuantity<="+stockoutMax);
        q.having(cancelMin!=null,"cancelQuantity>="+cancelMin);
        q.having(cancelMax!=null,"cancelQuantity<="+cancelMax);
        q.having(returnMin!=null,"returnQuantity>="+returnMin);
        q.having(returnMax!=null,"returnQuantity<="+returnMax);
        //订单审核时间、订单取消时间、订单断货时间、订单退货时间
        q.and(StrUtil.isNotEmpty(startTime),
                i->i.and(j->j.ge("date_format(o.audit_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.audit_time,'%Y-%m-%d')",endTime))
                        .or(z->z.ge("date_format(o.canceled_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.canceled_time,'%Y-%m-%d')",endTime))
                        .or(l->l.ge("date_format(o.nosupplly_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.nosupplly_time,'%Y-%m-%d')",endTime))
                        .or(m->m.ge("date_format(afterSale.audited_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(afterSale.audited_time,'%Y-%m-%d')",endTime)));
        //只筛选审核完成的订单
        q.isNotNull("o.audit_time");
        q.groupBy("goods.goods_id");
        return baseMapper.productSaleDetail(page,q);
    }

    @Override
    public IPage<ProductSaleVolumeVo> productSaleVolume(Long actId, String color, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("goods.goods_name,goods.goods_img,goods.partition_name,gg.goods_sys,gg.gs_season,goods.cat_name,goods.commodity_code" +
                ",goods.market_price,goods.price,goods.price*o.discount discountPrice" +
                ",group_concat(distinct goods.color ORDER BY goods.color Asc) color" +
                ",group_concat(distinct goods.size ORDER BY goods.size Asc) size" +
                ",count(if(o.sys_tag!="+ OrderSysTagEnum.YTHC_TAG_505.getValue() +" and o.sys_tag!="+ OrderSysTagEnum.DHK_TAG_402.getValue() +" and o.sys_tag!="+ OrderSysTagEnum.QXK_TAG_401.getValue() +",true,null)) quantity" +
                ",count(if(o.sys_tag="+ OrderSysTagEnum.DHK_TAG_402.getValue() +",true,null)) stockoutQuantity" +
                ",count(if(o.sys_tag="+ OrderSysTagEnum.QXK_TAG_401.getValue() +",true,null)) cancelQuantity" +
                ",count(if(o.sys_tag="+ OrderSysTagEnum.YTHC_TAG_505.getValue() +",true,null)) returnQuantity" +
                "");
        q.eq(actId!=null,"o.act_id",actId);
        q.like(StrUtil.isNotEmpty(color),"goods.color",color);
        //订单审核时间、订单取消时间、订单断货时间、订单退货时间
        q.and(StrUtil.isNotEmpty(startTime),
                i->i.and(j->j.ge("date_format(o.audit_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.audit_time,'%Y-%m-%d')",endTime))
                        .or(z->z.ge("date_format(o.canceled_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.canceled_time,'%Y-%m-%d')",endTime))
                        .or(l->l.ge("date_format(o.nosupplly_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(o.nosupplly_time,'%Y-%m-%d')",endTime))
                        .or(m->m.ge("date_format(afterSale.audited_time,'%Y-%m-%d')",startTime).le(StrUtil.isNotEmpty(endTime),"date_format(afterSale.audited_time,'%Y-%m-%d')",endTime)));
        //只筛选审核完成的订单
        q.isNotNull("o.audit_time");
        q.isNotNull("o.order_type");
        q.groupBy("goods.goods_id");
        return baseMapper.productSaleVolume(page,q);
    }

    @Override
    public IPage<ProductSaleVolumeDetailVo> productSaleVolumeDetail(Integer type, Long actId, String color, String startTime, String endTime, Page page) {
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.select("o.order_sn,goods.goods_name,goods.style_no,goods.color,goods.size,goods.partition_name" +
                ",o.order_type,express.contact_user,express.contact_phone,o.audit_time" +
                "");
        q.eq(actId!=null,"o.act_id",actId);
        //订单销量
        if (type!=null&&type==1){
            q.ne("o.sys_tag",OrderSysTagEnum.QXK_TAG_401.getValue());
            q.ne("o.sys_tag",OrderSysTagEnum.DHK_TAG_402.getValue());
            q.ne("o.sys_tag",OrderSysTagEnum.YTHC_TAG_505.getValue());
        }
        //断货
        if (type!=null&&type==2){
            q.eq("o.sys_tag",OrderSysTagEnum.DHK_TAG_402.getValue());
        }
        //取消
        if (type!=null&&type==3){
            q.eq("o.sys_tag",OrderSysTagEnum.QXK_TAG_401.getValue());
        }
        //退货
        if (type!=null&&type==4){
            q.eq("o.sys_tag",OrderSysTagEnum.YTHC_TAG_505.getValue());
        }
        q.like(StrUtil.isNotEmpty(color),"goods.color",color);
        q.ge(StrUtil.isNotEmpty(startTime),"date_format(o.audit_time,'%Y-%m-%d')",startTime);
        q.le(StrUtil.isNotEmpty(endTime),"date_format(o.audit_time,'%Y-%m-%d')",endTime);
        //只筛选审核完成的订单
        q.isNotNull("o.audit_time");
        q.isNotNull("o.order_type");
        return baseMapper.productSaleVolumeDetail(page,q);
    }
}
